/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PersonEditView.java
 *
 * Created on May 18, 2011, 10:18:22 AM
 */
package views;

import common.Md5;
import controllers.PersonController;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import models.Person;
import models.roles.AbstractRole;
import models.roles.Sponsor;

/**
 *
 * @author pelle
 */
public class PersonEditView extends AbstractView {
    
    // Controlleren som indeholder den person vi arbejder med lige nu
    PersonController personCtr = null;
    
    // Om vi retter en eksisterende bruger
    boolean isUpdating = false;
    
    // Oprette en ny person
    public PersonEditView() {
        initComponents();
        personCtr = new PersonController();
        
        createTxt.setVisible(true);
        editTxt.setVisible(false);
        
        isUpdating = true;
        setValues();
    }
    
    // Rette en person ud fra et id
    public PersonEditView(int personId) {
        initComponents();
        personCtr = new PersonController(personId);
        
        createTxt.setVisible(false);
        editTxt.setVisible(true);
        
        isUpdating = true;
        setValues();
    }
    
    // Rette en person ud fra et person-objekt
    public PersonEditView(Person person) {
        initComponents();
        personCtr = new PersonController(person);
        
        createTxt.setVisible(false);
        editTxt.setVisible(true);
        
        isUpdating = true;
        setValues();
    }
    
    // Metode til at smide værdier 
    private void setValues() {
        Person person = personCtr.getPerson();
        
        if(person != null) {
            enableSponsor(false);
            
            // Sætter felterne i GUI'en med værdierne fra personen
            this.name.setText(person.getName());
            this.address.setText(person.getAddress());
            this.zipcode.setText("" + person.getZipcode());
            this.city.setText(person.getCity());
            this.email.setText(person.getEmail());
            this.phone.setText(person.getPhone());

            if(person.getCpr() != null && person.getCpr().length() > 0) {
                this.birthday.setText(person.getCpr().substring(0, 6));
                this.cprcontrol.setText(person.getCpr().substring(6));
            }

            if(person.isMember()) {
                checkMember.setSelected(true);
            }

            if(person.isManager()) {
                checkManager.setSelected(true);
            }

            if(person.isTrainer()) {
                checkTrainer.setSelected(true);
            }

            if(person.isSponsor()) {
                Sponsor s = null;
                // Løber rollerne igennem for at finde sponsor-objektet
                for(AbstractRole a : person.getPersonRoles()) {
                    // Hvis den er sponsor, så typecaster vi objektet
                    if(a instanceof Sponsor) {
                        s = (Sponsor)a;
                    }
                }
                
                if(s != null) {
                    companyName.setText(s.getCompanyName());
                    website.setText(s.getWebsite());
                }
                
                checkSponsor.setSelected(true);
                enableSponsor(true);
            }

            if(person.isInspector()) {
                checkInspector.setSelected(true);
            }
            
            this.updateRelatedPersons();
            
        } else {
            this.showError("Brugeren kunne ikke findes i databasen!");
            dispose();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        address = new javax.swing.JTextField();
        email = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        zipcode = new javax.swing.JTextField();
        city = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        birthday = new javax.swing.JTextField();
        cprcontrol = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        phone = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        password2 = new javax.swing.JPasswordField();
        password1 = new javax.swing.JPasswordField();
        createTxt = new javax.swing.JLabel();
        editTxt = new javax.swing.JLabel();
        saveBtn = new javax.swing.JButton();
        sponsorPanel = new javax.swing.JPanel();
        website = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        companyName = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        checkInspector = new javax.swing.JCheckBox();
        checkTrainer = new javax.swing.JCheckBox();
        checkMember = new javax.swing.JCheckBox();
        checkManager = new javax.swing.JCheckBox();
        checkSponsor = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        relatedPersons = new javax.swing.JList();
        addPerson = new javax.swing.JButton();
        showPerson = new javax.swing.JButton();
        removePerson = new javax.swing.JButton();
        Annuller = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ret bruger");

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 18));
        jLabel2.setText("bruger");

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Oplysninger"));

        jLabel1.setText("Navn:");

        jLabel3.setText("Adresse:");

        jLabel4.setText("E-mail:");

        jLabel5.setText("Postnr. & by:");

        zipcode.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        zipcode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                zipcodeKeyReleased(evt);
            }
        });

        city.setText("<Indtast postnummer>");

        jLabel8.setText("CPR-nummer:");

        birthday.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        birthday.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                birthdayKeyReleased(evt);
            }
        });

        cprcontrol.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        cprcontrol.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cprcontrolKeyReleased(evt);
            }
        });

        jLabel9.setText("-");

        phone.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        phone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                phoneKeyReleased(evt);
            }
        });

        jLabel10.setText("Telefon nr.:");

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel8)
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jLabel10)
                        .add(51, 51, 51)
                        .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jPanel4Layout.createSequentialGroup()
                                .add(birthday, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel9))
                            .add(phone))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cprcontrol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel4)
                            .add(jLabel5)
                            .add(jLabel3)
                            .add(jLabel1))
                        .add(43, 43, 43)
                        .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(name, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
                            .add(jPanel4Layout.createSequentialGroup()
                                .add(zipcode, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(city, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))
                            .add(address, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
                            .add(email, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(name, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(address, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(zipcode, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(city))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(email, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(phone, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel10))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel8)
                    .add(birthday, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel9)
                    .add(cprcontrol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(75, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Ny adgangskode"));

        jLabel6.setText("Ny adgangskode:");

        jLabel7.setText("Gentag ny adgangskode:");

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel7)
                    .add(jLabel6))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(password2)
                    .add(password1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 182, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(password1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(password2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        createTxt.setFont(new java.awt.Font("Lucida Grande", 1, 18));
        createTxt.setText("Opret");

        editTxt.setFont(new java.awt.Font("Lucida Grande", 1, 18));
        editTxt.setText("Ret");

        saveBtn.setText("Gem");
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        sponsorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Sponsor oplysninger"));
        sponsorPanel.setName(""); // NOI18N

        jLabel11.setText("Hjemmeside:");

        jLabel12.setText("Virksomhedsnavn:");

        org.jdesktop.layout.GroupLayout sponsorPanelLayout = new org.jdesktop.layout.GroupLayout(sponsorPanel);
        sponsorPanel.setLayout(sponsorPanelLayout);
        sponsorPanelLayout.setHorizontalGroup(
            sponsorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(sponsorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(sponsorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel12)
                    .add(jLabel11))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sponsorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(website, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
                    .add(companyName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))
                .addContainerGap())
        );
        sponsorPanelLayout.setVerticalGroup(
            sponsorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(sponsorPanelLayout.createSequentialGroup()
                .add(sponsorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel12)
                    .add(companyName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sponsorPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel11)
                    .add(website, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(77, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Rettigheder"));

        checkInspector.setText("Inspektør");

        checkTrainer.setText("Træner");

        checkMember.setText("Medlem");

        checkManager.setText("Bestyrelse");

        checkSponsor.setText("Sponsor");
        checkSponsor.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                checkSponsorStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(checkTrainer)
                    .add(checkMember)
                    .add(checkManager)
                    .add(checkSponsor)
                    .add(checkInspector))
                .addContainerGap(140, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(checkMember)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(checkTrainer)
                .add(2, 2, 2)
                .add(checkManager)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(checkSponsor)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(checkInspector)
                .addContainerGap(9, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Relaterede personer"));

        relatedPersons.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        relatedPersons.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                relatedPersonsMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(relatedPersons);

        addPerson.setText("Tilføj");
        addPerson.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPersonActionPerformed(evt);
            }
        });

        showPerson.setText("Vis");
        showPerson.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showPersonActionPerformed(evt);
            }
        });

        removePerson.setText("Fjern");
        removePerson.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removePersonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(removePerson, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(addPerson, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .add(showPerson))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(addPerson)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removePerson))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 91, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(showPerson))
        );

        Annuller.setText("Afbryd");
        Annuller.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AnnullerActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(createTxt)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(editTxt)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel2))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(18, 18, 18)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(saveBtn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(Annuller, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(sponsorPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(25, 25, 25))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(createTxt)
                    .add(editTxt)
                    .add(jLabel2))
                .add(14, 14, 14)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(sponsorPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(layout.createSequentialGroup()
                            .add(Annuller)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(saveBtn))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void zipcodeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_zipcodeKeyReleased
        String zipcodeString = this.zipcode.getText();
        
        if(zipcodeString.length() >= 4) {
            zipcodeString = zipcodeString.substring(0,4);
            zipcode.setText(zipcodeString);
            
            String c = "<Ugyldigt postnummer!>";
            
            // Konvertere en String til heltal (i AbstractView)
            int zip = this.stringToInt(zipcodeString);
            if(zip > 0) {
                String ci = this.personCtr.getCity(zip);
                
                if(ci.length() > 0) {
                    c = ci;
                } else {
                    zipcode.setText("");
                }
                
            } else {
                this.showError("Postnummer må kun indeholde tal!");
            }
            
            this.city.setText(c);
        }
    }//GEN-LAST:event_zipcodeKeyReleased

    private void birthdayKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_birthdayKeyReleased
        String txt = birthday.getText();
        
        // Konvertere en String til heltal (i AbstractView)
        int cpr1 = this.stringToInt(txt);
        if(cpr1 == 0) {
            this.showError("CPR-nummer må kun indeholde tal!");
            birthday.setText("");
        }
        
        if(txt.length() >= 6) {
            birthday.setText(txt.substring(0,6));
            cprcontrol.grabFocus();
        }
}//GEN-LAST:event_birthdayKeyReleased

    private void cprcontrolKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cprcontrolKeyReleased
        String txt = cprcontrol.getText();
       
        // Konvertere en String til heltal (i AbstractView)
        int cpr2 = this.stringToInt(txt);
        if(cpr2 == 0) {
            this.showError("CPR-nummer må kun indeholde tal!");
            cprcontrol.setText("");
        }
        
        if(txt.length() > 4) {
            cprcontrol.setText(txt.substring(0,4));
        }
}//GEN-LAST:event_cprcontrolKeyReleased

    private void phoneKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_phoneKeyReleased
        String txt = phone.getText();
        
        // Konvertere en String til heltal (i AbstractView)
        int phoneInt = this.stringToInt(txt);
        if(phoneInt == 0) {
            this.showError("Telefonnummer må kun indeholde tal!");
            phone.setText("");
        }
        
        if(txt.length() > 8) {
            phone.setText(txt.substring(0,8));
        }
    }//GEN-LAST:event_phoneKeyReleased

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        String pw1 = "";
        String pw2 = "";
       
        // Tjekker om der er et password i begge felter
        if(password1.getPassword().length > 0 || password2.getPassword().length > 0 ) {
            pw1 = Md5.md5(this.passwordToString(password1.getPassword()));
            pw2 = Md5.md5(this.passwordToString(password2.getPassword()));
        }
        
        // Kontrollere om der er tastet et kodeord ind, og de 2 indtastede koder er ens
        // Hvis man opdatere en person, er det ikke nødvendigt at taste en kode ind igen
        if(isUpdating || (!isUpdating && password1.getPassword().length > 0 && pw1.equals(pw2))) {
        
            // Midlertidige variabler
            String nameString = name.getText();
            String addressString = address.getText();
            int zipcodeInt = this.stringToInt(zipcode.getText());
            String emailString = email.getText();
            String phoneString = phone.getText();
            String cpr = birthday.getText() + cprcontrol.getText();

            String sponsorName = "";
            String sponsorWebsite = "";
            
            // Hvis firmanavnet er aktiveret (sponsor er "hakket af")
            if(companyName.isEnabled()) {
                sponsorName = this.companyName.getText();
            }

            // Hvis website er aktiveret (sponsor er "hakket af")
            if(website.isEnabled()) {
                sponsorWebsite = this.website.getText();
            }

            // Ny liste til roller
            ArrayList<String> roles = new ArrayList<String>();

            if(checkMember.isSelected()) {
                roles.add("member");
            }

            if(checkTrainer.isSelected()) {
                roles.add("trainer");
            }

            if(checkManager.isSelected()) {
                roles.add("manager");
            }

            if(checkInspector.isSelected()) {
                roles.add("inspector");
            }

            if(checkSponsor.isSelected()) {
                roles.add("sponsor");
            }

            // Får en arrayliste tilbage med rollerne
            ArrayList<AbstractRole> personRoles = this.personCtr.createRoles(roles, sponsorName, sponsorWebsite);

            int res = this.personCtr.savePerson(nameString, addressString, zipcodeInt, emailString, phoneString, cpr, pw1, personRoles);
            if(res > 0) {
                if(this.isUpdating) {
                    this.showMessage("Personen er blevet opdateret!");
                } else {
                    this.showMessage("Personen er blevet oprettet med ID: #" + res);
                }
                
            } else {
                this.showError("Der opstod en fejl. Kunne ikke gemme personen.");
            }
        
        } else {
            this.showError("De 2 adgangskoder er ikke ens!");
        }
    }//GEN-LAST:event_saveBtnActionPerformed

    private void addPersonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPersonActionPerformed
        // Åbner et "vælg person" vindue, og sender sig selv med
        new PersonSelectView(this).setVisible(true);
    }//GEN-LAST:event_addPersonActionPerformed

    private void checkSponsorStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_checkSponsorStateChanged
        boolean checked = false;
        if(this.checkSponsor.isSelected()) {
            checked = true;
        }
        
        this.enableSponsor(checked);
    }//GEN-LAST:event_checkSponsorStateChanged

    private void AnnullerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnnullerActionPerformed
        // Lukker vinduet
        dispose();
    }//GEN-LAST:event_AnnullerActionPerformed

    private void relatedPersonsMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_relatedPersonsMouseReleased
        // Hvis der er dobbeltklikket på et element
        if(evt.getClickCount() == 2) {
            this.openRelatedPerson();
        }
    }//GEN-LAST:event_relatedPersonsMouseReleased

    private void removePersonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePersonActionPerformed
        int index = relatedPersons.getSelectedIndex();
        if(index > 0) {
            Person person = this.personCtr.getPerson().getContacts().get(index);
            if(person != null) {
                this.personCtr.removeRelatedPerson(person);
            }
        }
        
        this.updateRelatedPersons();
    }//GEN-LAST:event_removePersonActionPerformed

    private void showPersonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showPersonActionPerformed
        this.openRelatedPerson();
    }//GEN-LAST:event_showPersonActionPerformed

    // Åbne den valgte relaterede person
    private void openRelatedPerson() {
        int index = relatedPersons.getSelectedIndex();
        if(index >= 0) {
            Person person = this.personCtr.getPerson().getContacts().get(index);
            if(person != null) {
                new PersonEditView(person.getId()).setVisible(true);
            }
        }
    }
    
    // Metode til at gøre felterne for sponsor (de)aktive
    private void enableSponsor(boolean checked) {
        if(checked) {
            this.website.setEnabled(true);
            this.companyName.setEditable(true);
        } else {
            this.website.setEnabled(false);
            this.companyName.setEditable(false);
        }
    }
    
    // Metode som bliver kaldt fra "PersonSelectView" til at vide hvilken person der er valgt der
    public boolean addRelatedPerson(Person person) {
        boolean success = false;
        
        if(person != null) {
            if(personCtr.addRelatedPerson(person)) {
                
                this.updateRelatedPersons();
                success = true;
            }
        }
        
        return success;
    }
    
    // Opdatere listen med relaterede personer
    private void updateRelatedPersons() {
        // Tom liste til en GUI-list
        DefaultListModel listModel = new DefaultListModel();

        // Løber relaterede personer igennem
        for(Person p : personCtr.getPerson().getContacts()) {
            listModel.addElement(p.getName());
        }
        
        relatedPersons.setModel(listModel);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PersonEditView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Annuller;
    private javax.swing.JButton addPerson;
    private javax.swing.JTextField address;
    private javax.swing.JTextField birthday;
    private javax.swing.JCheckBox checkInspector;
    private javax.swing.JCheckBox checkManager;
    private javax.swing.JCheckBox checkMember;
    private javax.swing.JCheckBox checkSponsor;
    private javax.swing.JCheckBox checkTrainer;
    private javax.swing.JLabel city;
    private javax.swing.JTextField companyName;
    private javax.swing.JTextField cprcontrol;
    private javax.swing.JLabel createTxt;
    private javax.swing.JLabel editTxt;
    private javax.swing.JTextField email;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField name;
    private javax.swing.JPasswordField password1;
    private javax.swing.JPasswordField password2;
    private javax.swing.JTextField phone;
    private javax.swing.JList relatedPersons;
    private javax.swing.JButton removePerson;
    private javax.swing.JButton saveBtn;
    private javax.swing.JButton showPerson;
    private javax.swing.JPanel sponsorPanel;
    private javax.swing.JTextField website;
    private javax.swing.JTextField zipcode;
    // End of variables declaration//GEN-END:variables
}
